<template>
    <h2>借款修改页面</h2>
    <table>
        <tr>
            <td>借款款项名称:</td>
            <td>
                <input type="text" v-model="BName" />
            </td>
        </tr>
        <tr>
            <td>借款最高金额:</td>
            <td>
                <input type="text" v-model="BPrice" />
            </td>
        </tr>
        <tr>
            <td>借款最高年限:</td>
            <td>
                <input type="text" v-model="BYear" />
            </td>
        </tr>
        <tr>
            <td>利息编号:</td>
            <td>
                <select v-model="Iid">
                    <option v-for="i in data" :key="i.Iid" :value="i.Iid">{{ i.IYear }}年，利息为：{{ i.Ilixi }}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>借款款项简介:</td>
            <td>
                <input type="text" v-model="BInfo" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <button @click="add">修改</button>
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import axios from 'axios'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const BName = ref('')
const BPrice = ref()
const BYear = ref('')
const Iid = ref('')
const BInfo = ref('')
const data: any = ref([])
onMounted(() => {
    bind();
    fan();
})
const fan = () => {
    axios.get("http://localhost:55817/api/Tian/FanBorrow", {
        params: {
            id: route.params.BId
        }
    })
        .then(res => {
            BName.value = res.data.BName;
            BPrice.value = res.data.BPrice;
            BYear.value = res.data.BYear;
            Iid.value = res.data.Iid;
            BInfo.value = res.data.BInfo;
        })
        .catch(err => {
            console.log(err);
        })
}
const bind = () => {
    axios.get("http://localhost:55817/api/Tian/IntestBind")
        .then(res => {
            data.value = res.data;
        })
        .catch(err => {
            console.log(err);
        })
}
const add = () => {
    if (BPrice.value < 0) {
        alert("最高借款金额为数字且必须大于0");
        return;
    }
    if (BName.value == "" || BPrice.value == "" || BYear.value == "" || Iid.value == "" || BInfo.value == "") {
        alert("不能为空");
        return;
    }
    axios.post("http://localhost:55817/api/Tian/XiuBorrow", {
        BName: BName.value,
        BPrice: BPrice.value,
        BYear: BYear.value,
        Iid: Iid.value,
        BInfo: BInfo.value,
        BId: route.params.BId
    })
        .then(res => {
            if (res.data > 0) {
                alert("修改成功")
                router.push("/ShowBorrow")
            }
            else {
                alert("修改失败")
                return;
            }
        })
        .catch(err => {
            console.log(err);
        })
}
</script>